<template>
  <el-dropdown trigger="click" @command="handleSetSize">
    <div>
      <svg-icon class-name="size-icon" icon-class="size" />
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item v-for="item of sizeOptions" :key="item.value" :disabled="size===item.value" :command="item.value">
          {{ item.label }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup lang="ts">
import { computed, nextTick } from 'vue'
import { useAppStore } from '@/store'
import { useTagsViewStore } from '@/store/tagsView'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const appStore = useAppStore()
const tagsViewStore = useTagsViewStore()
const route = useRoute()
const router = useRouter()

const sizeOptions = [
  { label: 'Default', value: 'default' },
  { label: 'Medium', value: 'medium' },
  { label: 'Small', value: 'small' },
  { label: 'Mini', value: 'mini' }
]

const size = computed(() => appStore.size)

const handleSetSize = (size: string) => {
  const element = window as any
  element.$ELEMENT = element.$ELEMENT || {}
  element.$ELEMENT.size = size
  appStore.setSize(size)
  refreshView()
  ElMessage({
    message: 'Switch Size Success',
    type: 'success'
  })
}

const refreshView = () => {
  tagsViewStore.delAllViews()
  const { fullPath } = route

  nextTick(() => {
    router.replace({
      path: '/redirect' + fullPath
    })
  })
}
</script>
